<?php

class WebUser extends CWebUser {

//    const ARTISTROLEID = Roles::ARTIST;
//    const COMPANYROLEID = Roles::COMPANY;
//    const ARTISTID = 'id';
//    const COMPANYID = 'id';

    public $loginUrl = array('/user/user/login');

    /**
     * Performs access check for this user.
     * @param string the name of the role that need access check.
     * @param array name-value pairs that would be passed to business rules associated
     * with the tasks and roles assigned to the user.
     * @param boolean whether to allow caching the result of access checki.
     * This parameter has been available since version 1.0.5. When this parameter
     * is true (default), if the access check of an operation was performed before,
     * its result will be directly returned when calling this method to check the same operation.
     * If this parameter is false, this method will always call {@link CAuthManager::checkAccess}
     * to obtain the up-to-date access result. Note that this caching is effective
     * only within the same request.
     * @return boolean whether the operations can be performed by this user.
     */
    public function checkAccess($operation, $params = array(), $allowCaching = true) {
        return $this->hasRole($operation);
    }

    /**
     * Checks if this (non-admin) User can administrate some users
     */
    public static function hasUsers($uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        $user = CActiveRecord::model('User')->findByPk($uid);

        return isset($user->users);
    }

    /**
     * Checks if this (non-admin) User can administrate the given user
     */
    public static function hasUser($username, $uid = 0) {

        if ($uid == 0)
            $uid = Yii::app()->user->getId();

        // Every user can modify himself
        if ($username == $uid)
            return true;

        $user = CActiveRecord::model('User')->findByPk($uid);

        if (!is_array($username))
            $username = array($username);

        if (isset($user->users))
            foreach ($user->users as $userobj) {
                if (in_array($userobj->username, $username) ||
                        in_array($userobj->id, $username))
                    return true;
            }
        return false;
    }

   
    public static function hasRole($role, $uid = 0) {
        if ($uid == 0)
            $uid = Yii::app()->user->getId();


        if (!is_array($role))
            $role = array($role);

        $user = CActiveRecord::model('User')->findByPk($uid);
       
        if (isset($user->roles))
            foreach ($user->roles as $roleobj) {
                if (in_array($roleobj->title, $role) ||
                        in_array($roleobj->id, $role))
                    return true;
            }
        return false;
    }

    /**
     * Return admin status.
     * @return boolean
     */
    public function isAdmin() {
        if ($this->isGuest)
            return false;
        else {

            if (User::model()->active()->superuser()->findbyPk(Yii::app()->user->id))
                return true;
            else
                return false;
        }
    }

    public function afterLogin($fromCookie) {
       // $this->setIdOrOtherInSession();
        parent::afterLogin($fromCookie);
    }

    public function setIdOrOtherInSession() {
        $session = new CHttpSession;

        $model = False;
        $uid = Yii::app()->getUser()->getId();
        if (Yii::app()->getUser()->hasRole(self::ARTISTROLEID)) {
            $user_role_id = self::ARTISTROLEID;
            $attribute = self::ARTISTID;
            $model = Artist::model()->find('user_id=:user_id', array(':user_id' => $uid));

        } elseif (Yii::app()->user->hasRole(self::COMPANYROLEID)) {
            $user_role_id = self::COMPANYROLEID;
            $attribute = self::COMPANYID;
            $model = Company::model()->find('users_id=:users_id', array(':users_id' => $uid));
        }

        if ($model) {
            $session->add($attribute, $model->getAttribute($attribute));
            if (!empty($user_role_id)) {
                //$session->add(Roles::USER_ROLE_ID, $user_role_id);
            }
        }
    }

}

?>
